package com.jcli.renameFiles;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class FileRenamerGUI extends JFrame {
    private JTextField folderPathField;
    private JTextField charCountField;
    private JButton renameButton;

    public FileRenamerGUI() {
        setTitle("File Renamer");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null); // 将窗体放置在屏幕中央
        setResizable(false); // 禁用改变大小
        JPanel mainPanel = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        JLabel folderPathLabel = new JLabel("Folder Path:");
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(folderPathLabel, gbc);

        // 自定义文本框大小
        folderPathField = new JTextField();
        folderPathField.setToolTipText("Enter folder path here");
        folderPathField.setPreferredSize(new Dimension(200, 25));
        gbc.gridx = 1;
        mainPanel.add(folderPathField, gbc);

        JLabel charCountLabel = new JLabel("Character Count to Remove:");
        gbc.gridx = 0;
        gbc.gridy = 1;
        mainPanel.add(charCountLabel, gbc);

        // 自定义文本框大小
        charCountField = new JTextField();
        charCountField.setToolTipText("Enter character count to remove");
        charCountField.setPreferredSize(new Dimension(200, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        mainPanel.add(charCountField, gbc);

        renameButton = new JButton("Rename Files");
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        mainPanel.add(renameButton, gbc);

        add(mainPanel);

        renameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                renameFiles();
            }
        });
    }

    private void renameFiles() {
        String folderPath = folderPathField.getText();
        String charCountStr = charCountField.getText();
        int charCountToRemove = 0;

        try {
            charCountToRemove = Integer.parseInt(charCountStr);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Invalid character count.");
            return;
        }

        File folder = new File(folderPath);

        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();

            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        String oldName = file.getName();

                        if (oldName.length() > charCountToRemove) {
                            String newName = oldName.substring(charCountToRemove);

                            File newFile = new File(folderPath, newName);

                            if (file.renameTo(newFile)) {
                                System.out.println("File " + oldName + " renamed to " + newName);
                            } else {
                                System.out.println("Failed to rename file " + oldName);
                            }
                        }
                    }
                }
                JOptionPane.showMessageDialog(this, "Files renamed successfully.");
            } else {
                JOptionPane.showMessageDialog(this, "Folder does not contain any files.");
            }
        } else {
            JOptionPane.showMessageDialog(this, "Invalid folder path.");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                FileRenamerGUI gui = new FileRenamerGUI();
                gui.setVisible(true);
            }
        });
    }
}
